package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.constant.RedisConstant;
import com.tbit.uqbike.client.constant.ReturnBikeMsgLogConstant;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.constant.SystemData;
import com.tbit.uqbike.client.dao.log.ReturnBikeMsgLogDao;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.MachineBorrow;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.ReturnBikeMsgLog;
import com.tbit.uqbike.client.service.CacheService;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.service.ReturnBikeMsgLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:BUGTian
 * @DATE: 2023/6/1
 */
@Service
@Slf4j
public class ReturnBikeMsgLogServiceImpl implements ReturnBikeMsgLogService {

    @Autowired
    private CacheService cacheService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private ReturnBikeMsgLogDao returnBikeMsgLogDao;
    @Autowired
    private RedisService redisService;

    @Override
    public void addBikeMsgLog(MachineBorrow machineBorrow, Machine machine, Integer posFrom, Integer returnType, Integer parkPointId, Map<String, Double> distanceMap, Date returnTime, Point checkPoint) {
        ReturnBikeMsgLog returnBikeMsgLog = new ReturnBikeMsgLog();
        Integer brandId = cacheService.getBrandId(machine.getAccountId());
        returnBikeMsgLog
                .setOrderNO(machineBorrow.getOrderNO())
                .setBrandId(brandId)
                .setAccountId(machine.getAccountId())
                .setUserId(machineBorrow.getUserId())
                .setUserCode(machine.getUserCode())
                .setMachineNO(machine.getMachineNO())
                .setPosFrom(posFrom)
                .setReturnType(returnType)
                .setParkpointId(parkPointId)
                .setDistance(distanceMap == null ? null : distanceMap.get("returnDistance"))
                .setReturnTime(returnTime)
                .setLon(checkPoint.getX())
                .setLat(checkPoint.getY());
        if (checkPoint.getX() != null && checkPoint.getY() != null) {
            Point calibrate = calibrateService.calibrate(checkPoint, SysConstant.ORIGINAL, SysConstant.BAIDU);
            returnBikeMsgLog.setLonC(calibrate.getX())
                    .setLatC(calibrate.getY());
        }
        if (ReturnBikeMsgLogConstant.TYPE_PURPOSE.equals(returnType)) {
            redisService.add(RedisConstant.RETURN_BIKE_MSG_LOG_PREFIX + machineBorrow.getOrderNO(), JSON.toJSONString(returnBikeMsgLog), 120);
        }
        if (ReturnBikeMsgLogConstant.TYPE_ACTUAL.equals(returnType)) {
            String s = redisService.get(RedisConstant.RETURN_BIKE_MSG_LOG_PREFIX + machineBorrow.getOrderNO());
            if (StringUtils.hasText(s)) {
                returnBikeMsgLog = JSON.parseObject(s, ReturnBikeMsgLog.class);
            }
            returnBikeMsgLog.setReturnTime(returnTime).setReturnType(ReturnBikeMsgLogConstant.TYPE_ACTUAL);
        }
        SystemData.returnBikeMsgLogList.add(returnBikeMsgLog);
    }

    @Override
    public void addBatch(List<ReturnBikeMsgLog> list) {
        log.info("还车数据分析日志批量入库{}", list.size());
        List<List<ReturnBikeMsgLog>> split = ListUtil.split(list, 500);
        for (List<ReturnBikeMsgLog> returnBikeMsgLogs : split) {
            returnBikeMsgLogDao.insertBatch(returnBikeMsgLogs);
        }
    }


}
